package com.geek.base.file.controller;

import com.geek.base.common.bean.ResponseBean;
import com.geek.base.file.bean.FileBean;
import com.geek.base.file.bean.SftpBean;
import com.geek.utils.SftpPoolUtil;
import com.jcraft.jsch.ChannelSftp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/file/sftp")
public class SftpController {

    @RequestMapping("/index")
    public ModelAndView fileIndex(ModelAndView mv) {
        mv.setViewName("/file/sftp/index");
        return mv;
    }

    @ResponseBody
    @RequestMapping("/getFileList")
    public ResponseBean getFileList(String filePath) {
        ResponseBean responseBean = new ResponseBean();
        try {
            SftpBean sftpBean = new SftpBean("zuoshuai.iok.la", 22679, "shuai", "root1993");
            ChannelSftp sftp = SftpPoolUtil.openSftpConnect(sftpBean);
            List<FileBean> fileList = SftpPoolUtil.getWebFileList(SftpPoolUtil.listFile(filePath, sftp));
            responseBean.setData(fileList);
        } catch (Exception e) {
            responseBean.setCode(500);
            responseBean.setMsgs(e.getMessage());
        }
        return responseBean;
    }

}
